package club.justwill.single.Controller;

import club.justwill.single.Service.OrderService;
import club.justwill.single.Service.ProjectIntroduceService;
import club.justwill.single.Service.ProjectsService;
import club.justwill.single.aspect.LoginRequired;
import club.justwill.single.assembler.ProjectAssemble;
import club.justwill.single.assembler.ProjectIntroduceAssemble;
import club.justwill.single.assembler.SkusAssemble;
import club.justwill.single.bean.*;
import club.justwill.single.model.CoreConstants;
import club.justwill.single.model.JsonResponse;
import club.justwill.single.model.SecurityToken;
import club.justwill.single.persistence.dao.ProjectDao;
import club.justwill.single.persistence.dao.SkuDao;
import club.justwill.single.persistence.jpo.ProjectIntroduce;
import club.justwill.single.persistence.jpo.Projects;
import club.justwill.single.persistence.jpo.Skus;
import club.justwill.single.util.JsonResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.util.List;

/**
 * Created by luoyu on 15-12-27.
 */
@RestController
@Scope(WebApplicationContext.SCOPE_SESSION)
public class ProjectIntroduceController extends BaseController implements Serializable {
    public static Logger logger = LoggerFactory.getLogger(ProjectIntroduceController.class);
    @Autowired
    private ProjectIntroduceService projectIntroduceService;
    @Autowired
    private ProjectIntroduceAssemble piAssemble;
    @Autowired
    private ProjectDao projectDao;
    @Autowired
    private ProjectAssemble projectAssemble;
    @Autowired
    private SkuDao skuDao;
    @Autowired
    private SkusAssemble skusAssemble;
    @Autowired
    private OrderService orderService;

    @LoginRequired
    @RequestMapping(value = URL_PROJECT_INTRODUCE_CREATE, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse createProjectIntroduce(@RequestBody CreateProjectIntroduce createProjectIntroduce){
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        ProjectIntroduce projectIntroduce = projectIntroduceService.addProjectIntroduce(token,createProjectIntroduce,obj);

        if(JsonResponseHelper.isHasError(obj)){
            obj.setFailed();
        }else {
            ProjectIntroduceResp piResp = piAssemble.assembleProjectsIntroduce(projectIntroduce);
            obj.setData(piResp);
            obj.setSuccessful();
        }
        return obj;

    }

    @LoginRequired
    @RequestMapping(value = URL_PROJECT_INTRODUCE_UPDATE, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse updateProjectIntroduce(@RequestBody UpdateProjectIntroduce updateProjectIntroduce){
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        projectIntroduceService.updateProjectIntroduce(token,updateProjectIntroduce,obj);
        if(JsonResponseHelper.isHasError(obj)){
            obj.setFailed();
        }else {
            obj.setSuccessful();
        }
        return obj;

    }

    @RequestMapping(value = URL_PROJECT_INTRODUCE_SEARCH, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse searchProjectIntruduce(@RequestBody SearchProjectIntroduce searchProjectIntroduce){
        JsonResponse obj = new JsonResponse();
        List<ProjectIntroduce> projectIntroduces = projectIntroduceService.searchProjectIntroduce(searchProjectIntroduce,obj);
        obj.setData(piAssemble.assembleProjectsIntroduces(projectIntroduces));
        obj.setSuccessful();
        return obj;
    }

    @RequestMapping(value = URL_PROJECT_SUPPORT_RETURN_SEARCH, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse searchProjectSupportReturn(@PathVariable long projectId) {
        JsonResponse obj = new JsonResponse();
        Projects projects = projectDao.getByProjectId(projectId);
        ProjectsResp projectsResp = projectAssemble.assembleProject(projects);
        /*projectsResp.setOrdersRespList(orderService.searchOrdersRespByProjectId(projectId,obj));*/
        /*List<Skus> skusList = skuDao.getSkusByProjectId(projectId);
        List<SkusResp> skusRespList = skusAssemble.assembleSkusList(skusList);
        projectsResp.setSkusList(skusRespList);*/
        List<ProjectIntroduce> supportReturnList = projectIntroduceService.searchProjectSupportReturn(projectId,obj);
        if (JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        }else {
            projectsResp.setIntroduceList(piAssemble.assembleProjectsIntroduces(supportReturnList));
            obj.setData(projectsResp);
            obj.setSuccessful();
        }
        return obj;
    }

}
